package com.gxwz.document.business.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.gxwz.document.business.util.SystemContext;
import com.gxwz.document.business.entity.Literature;
import com.gxwz.document.business.service.IliteratureService;
import com.gxwz.document.core.action.BaseAction;
import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("serial")
public class UploadAction extends BaseAction {
	
	protected static final String LIST_JSP = "/WEB-INF/page/literatureManage/Literature_list.jsp";
	
	private String title;
	private File upload;
	private String uploadFileName;
	private String uploadContentType;
	private String savePath;
	private String litType;//文件类型
	private String litGrade;
	private String litTheme;
	private Date  litUploadTime;
	private double litSize;
	private List<Literature> ListLit;
	private ServletContext context;
	private Literature lit;
	@Autowired
	private IliteratureService litService;

	
	private int totalRow  = 0;
	
	//清理第一次信息，每次就都会有第一次，不会产生第二次，非常重要啵，ok啦--当第一次失败之后，以后所有的连接就都会返回input的页面
    @Override  
    public void validate() {  
        super.validate();  
        clearFieldErrors();  
    }  
    
	public String upload() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String litGrade=request.getParameter("lit.litGrade");
		String litUploadTime= request.getParameter("lit.litUploadTime");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date datee=sdf.parse(litUploadTime);
		String litTheme=request.getParameter("lit.litTheme");	
		@SuppressWarnings("resource")
		//以服务器的文件保存地址和原文件的名  建立上传文件输出流
		FileOutputStream fos = new FileOutputStream(this.getSavePath()+"\\"+this.getUploadFileName());
		
		//以上传文件建立一个文件上传流
		FileInputStream fis = new FileInputStream(this.getUpload());
		//将上传文件的内容写入服务器
		byte [] buffer = new byte[1024];
		int len=0;
		while((len=fis.read(buffer))>0){
			fos.write(buffer, 0, len);
		}
		fos.close();
		fis.close();
		/*litType获取文件个数而已*/
		 int pos = uploadFileName.lastIndexOf( "." ); 
		 String litType = String.valueOf(pos);
		 //文献大小
		 InputStream in =new FileInputStream(this.getSavePath()+"\\"+this.getUploadFileName());
		 litSize =in.available();
		 litSize /= 1024;
		 
		/*//将信息保存到数据库
		String url = this.getSavePath();
		//取得最后一个/的下标  
    	  int index = url.lastIndexOf(".");  
    	  //将字符串转为字符数组  
    	  char[] ch = url.toCharArray();  
		 //根据 copyValueOf(char[] data, int offset, int count) 取得最后一个字符串  
    	String lastString = String.copyValueOf(ch, index + 1, ch.length - index - 1);
		litType=lastString;*/
		
		litService.saveFileInfo(uploadFileName,savePath,litType,uploadContentType,litGrade,litTheme,datee,litSize);//保存到文件表
		
		return openlist();

	}
	
	/**
	 * 文献列表分页查询
	 * @return
	 */
	public String openlist()throws Exception{
		row = (page-1)*DEFUALT_PAGE_SIZE; 
		totalRow = litService.getRowNum();
		if(page>(litService.getRowNum()/DEFUALT_PAGE_SIZE+1)){
			page--;
		}
		if(row > litService.getRowNum() || maxpage == 1){
			setListLit(litService.findByPage((litService.getRowNum()-(litService.getRowNum()%DEFUALT_PAGE_SIZE)),DEFUALT_PAGE_SIZE));
		}else {
			setListLit(litService.findByPage(row,DEFUALT_PAGE_SIZE));
		}
		setForwardView(LIST_JSP);
		return SUCCESS;
	}
	
	@SuppressWarnings("deprecation")
	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	
	public String getLitGrade() {
		return litGrade;
	}

	public void setLitGrade(String litGrade) {
		this.litGrade = litGrade;
	}

	public Date getLitUploadTime() {
		return litUploadTime;
	}

	public void setLitUploadTime(Date litUploadTime) {
		this.litUploadTime = litUploadTime;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public Literature getLit() {
		return lit;
	}

	public void setLit(Literature lit) {
		this.lit = lit;
	}

	public String getLitTheme() {
		return litTheme;
	}

	public void setLitTheme(String litTheme) {
		this.litTheme = litTheme;
	}

	public List<Literature> getListLit() {
		return ListLit;
	}

	public void setListLit(List<Literature> listLit) {
		ListLit = listLit;
	}
}
